import { defineStore } from "pinia";
import { ref, reactive } from "vue"
import { ElMessage } from "element-plus";
import { useEnumStore } from "@/store/enumeration"
import { useComListData } from "@/store/list"
import { queryDictInfoApiGet,dictPagePost,dictDeleteDel } from "@/api/operationManagement"
import useInstance from '@/hooks/useInstance';
export const envSceneStore = defineStore("envSceneStoreId", () => {
    const { global } = useInstance();
    const addSonEnvironmentRef = ref<{ show: (title: string, row?: any) => void }>()
    const addSonSceneRef = ref<{ show: (title: string, row?: any) => void }>()
    const { getSceneList,getEnvList } = useComListData()

    const tableProp = reactive(
        {
            name: "envList",
            propsList: [

                {
                    label: 'Id',
                    prop: 'id',
                    type: 'normal',
                    width: '100px',
                    hidden: true,
                },
                {
                    label: '环境名称',
                    prop: 'dictItemName',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    label: '字典项编码',
                    prop: 'dictItemCode',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    type: 'handle',
                    label: '操作',
                    width: '180px',
                    btnType: 'link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })

    /* env */
    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            dictCode: 'ENVIRONMENT'
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []

    })
    const getDate1Env = async (type: string) => {
        pageParams.loadIng = true
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            
        }

        try {
            const res = await dictPagePost(listSearch)
            pageParams.loadIng = false
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false

        }
    }

    const handPageEnv = (params: any) => {
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1Env('search')
    }
    const delBtnEnv = async (type:number,row: any) => {
        const flag = await global.$myconfirm(`是否确定删除该数据？  ${row.dictItemName}`)
        if (flag) {
            const res = await dictDeleteDel({ id: row.id })
            console.log(res.data);
            if (res.code == 20000) {
                ElMessage({
                    type: "success",
                    message: "数据删除成功"
                })
                if(type==1){
                    getDate1Env('search')
                    getEnvList()
                }else{
                    getDate1Scene('search')
                    getSceneList()
                }
            } else {
                ElMessage({
                    type: "error",
                    message: "数据删除失败"
                })
            }
        }
    }

    /* scene */
    const tablePropScene = reactive(
        {
            name: "sceneList",
            propsList: [

                {
                    label: 'Id',
                    prop: 'id',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    label: '场景名称',
                    prop: 'dictItemName',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    label: '场景编码',
                    prop: 'dictItemCode',
                    type: 'normal',
                    minWidth: '200px',
                    hidden: true,
                },
                {
                    type: 'handle',
                    label: '操作',
                    minWidth: '180px',
                    btnType: ' link',
                    slotName: 'handle',
                    hidden: true,
                }
            ]
        })
    const listSearchScene = reactive({
        orders: ["created_time desc"],
        condition: {
            dictCode: 'SCENE'
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParamsScene = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []

    })
    const getDate1Scene = async (type: string) => {
        pageParamsScene.loadIng = true
        if (type == 'reset') {
            listSearchScene.pageNum = 1
            listSearchScene.pageSize = 20
     
        }

        try {
            const res = await dictPagePost(listSearchScene)
            pageParamsScene.loadIng = false
            if (res.code == 20000) {
                pageParamsScene.tableData = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                    }
                })
                pageParamsScene.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParamsScene.loadIng = false

        }
    }
    
    const handPageScene = (params: any) => {
        listSearchScene.pageNum = params.pageNum
        listSearchScene.pageSize = params.pageSize
        getDate1Scene('search')
    }

    return {
        tableProp, listSearch, pageParams, addSonEnvironmentRef,
        tablePropScene, listSearchScene, pageParamsScene, addSonSceneRef,
        getDate1Env, handPageEnv, delBtnEnv,
        getDate1Scene,handPageScene
    }
})